package com.creaeteboardanimation;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class ExpandingRectangleView extends SurfaceView implements SurfaceHolder.Callback{
	/**** Members *******************************************************/
	
	private ExpandingRectangleThread thread = null;
	private SurfaceHolder surfaceHolder = null;
	
	/**** Constructors **************************************************/
	public ExpandingRectangleView(Context context, AttributeSet attrs){
		super(context, attrs);
		
		this.setBackgroundColor(Color.TRANSPARENT);                 
	    this.setZOrderOnTop(true); //necessary                
	    getHolder().setFormat(PixelFormat.TRANSPARENT); 
		
		
		surfaceHolder = this.getHolder();
		surfaceHolder.addCallback(this);
		
		thread = new ExpandingRectangleThread(context);
		
		this.setFocusable(true);
		
	}
	
	/**** View Overrides ***********************************************/

	@Override
	public void onSizeChanged (int w, int h, int oldw, int oldh){
		thread.setCanvasSize(w, h);
	}
	
	@Override
	public void onWindowFocusChanged(boolean hasWindowFocus){
		super.onWindowFocusChanged(hasWindowFocus);
		
		if(hasWindowFocus){
			
		}
	}
	
	/**** SurfaceHoldler Overrides **************************************/
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		thread.setCanvasSize(width, height);
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// Start the thread
		thread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// we have to tell thread to shut down & wait for it to finish, or else
        // it might touch the Surface after we return and explode
        boolean retry = true;
        
        thread.setIsRunning(false);
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
            	
            }
        }
		
	}
	
	
	
	/********************************************************************
	 * 		Thread Subclass
	 */
	class ExpandingRectangleThread extends Thread{
		/**** Members ***************************/
		private Context context = null;
		
		private volatile boolean isRunning = true;
		private volatile boolean isRedraw = false;
		
		//private volatile int rectangleWidth = 50;
		//private volatile int canvasHeight = 50;
		private volatile PointF topLeftCorner = null;
		private volatile PointF bottomRightCorner = null;
		
		private Paint recPaint = null;
		
		/**** Constructor ***********************/
		public ExpandingRectangleThread(Context context){
			this.context = context;
			
			recPaint = new Paint();
			recPaint.setAntiAlias(true);
			recPaint.setARGB(0xf5, 0x00
					, 0x00, 0xff);
		}
		
		/**** Methods ***************************/
		
		private void drawRectangle(Canvas c){
			
			c.drawColor(0x00000000);
			
			c.drawRect(topLeftCorner.x, topLeftCorner.y,
						bottomRightCorner.x, bottomRightCorner.y, recPaint);
			
			postInvalidate();
		}
		
		public synchronized void setTopLeftCorner(float x, float y){
			topLeftCorner = new PointF(x,y);
			isRedraw = true;
		}
		
		public synchronized void setBottomRightCorner(float x, float y){
			bottomRightCorner = new PointF(x,y);
			isRedraw = true;
		}
		
		public synchronized void setCanvasSize(int width, int height){
			synchronized(surfaceHolder){
			
				float quarW = 0.25f * (float)width;
				float quarH = 0.25f * (float)height;
				topLeftCorner = new PointF(quarW,3*quarH);
				bottomRightCorner = new PointF(3*quarW,quarH);

				isRedraw = true;
			}
		}
		
		public void setIsRunning(boolean isRunning){
			this.isRunning = isRunning;
		}
		
		/**** Thread Overrides ******************/
		@Override
		public void run(){
			
			while(isRunning){
				if(isRedraw){
					isRedraw = false;
					
					Canvas c = null;					
					try{
						c = surfaceHolder.lockCanvas(null);
						
						synchronized(surfaceHolder){
							drawRectangle(c);
						}
					} finally {
						if(c != null){
							surfaceHolder.unlockCanvasAndPost(c);
						}
					}					
				}
			}
		}
	}
}
